/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package acousticfield3d.gui.misc;

import acousticfield3d.gui.MainForm;
import acousticfield3d.math.M;
import acousticfield3d.math.Vector3f;
import acousticfield3d.simulation.Transducer;
import acousticfield3d.utils.Parse;
import java.util.ArrayList;

/**
 *
 * @author am14010
 */
public class AddRadialTransducersForm extends javax.swing.JFrame {
    final MainForm mf;
    
    public AddRadialTransducersForm(MainForm mf) {
        this.mf = mf;
        initComponents();
    }

    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        amountText = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        radiousText = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        inclinationText = new javax.swing.JTextField();
        createButton = new javax.swing.JButton();
        deleteAllButton = new javax.swing.JButton();
        alignTransducersCheck = new javax.swing.JCheckBox();
        doubleBowlCheck = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Amount:");

        amountText.setText("1,6,12,18,24");

        jLabel2.setText("Radious:");

        radiousText.setText("0.10");

        jLabel3.setText("Incli:");

        inclinationText.setText("4");

        createButton.setText("Create");
        createButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createButtonActionPerformed(evt);
            }
        });

        deleteAllButton.setText("Delete all");
        deleteAllButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteAllButtonActionPerformed(evt);
            }
        });

        alignTransducersCheck.setText("align transducers");

        doubleBowlCheck.setText("double bowl");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(37, 37, 37)
                        .addComponent(inclinationText, javax.swing.GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(amountText)
                        .addGap(1, 1, 1))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(radiousText, javax.swing.GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(createButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(deleteAllButton))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(alignTransducersCheck)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(doubleBowlCheck)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(amountText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(radiousText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(inclinationText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(8, 8, 8)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(doubleBowlCheck)
                    .addComponent(alignTransducersCheck))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(createButton)
                    .addComponent(deleteAllButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void createButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createButtonActionPerformed
        final int[] amount = Parse.parseIntArray(amountText.getText(), ",");
        final float radious = Parse.toFloat( radiousText.getText() );
        final float inc = Parse.toFloat( inclinationText.getText() ) * M.DEG_TO_RAD;
        final boolean doubleBowl = doubleBowlCheck.isSelected();
        final boolean alignTransducers = alignTransducersCheck.isSelected();
        
        final int n = amount.length;
        for (int i = 0; i < n; ++i){
            final int trans = amount[i];
            for(int j = 0; j < trans; ++j){
                final Transducer t = createTransducer();
                final Vector3f pos = t.getTransform().getTranslation();
                
                float angle = M.TWO_PI * j / trans;
                if (alignTransducers && i % 2 == 0){
                    angle += M.TWO_PI / trans /2.0f;
                }
                pos.fromPolar(radious, angle, inc * i);
                pos.y *= -1;
                
                t.pointToTarget( Vector3f.ZERO );
                addTransducer(t);
                
                if (doubleBowl){
                   final Transducer t2 = createTransducer();
                   t2.getTransform().getTranslation().set( pos ).multLocal(1, -1, 1);
                   t2.pointToTarget( Vector3f.ZERO );
                   addTransducer(t2);
                }
            }
        }
        mf.needUpdate();
    }//GEN-LAST:event_createButtonActionPerformed

    private void deleteAllButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteAllButtonActionPerformed
        mf.clearSelection();
        final ArrayList<Transducer> copy = new ArrayList<>(mf.simulation.transducers);
        mf.transPanel.deleteTransducers( copy );
        mf.needUpdate();
    }//GEN-LAST:event_deleteAllButtonActionPerformed


    private Transducer createTransducer(){
        return mf.addTransducersForm.createTransducer();
    }
    
    private void addTransducer(Transducer t){
        mf.addTransducersForm.addTransducer(t);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox alignTransducersCheck;
    private javax.swing.JTextField amountText;
    private javax.swing.JButton createButton;
    private javax.swing.JButton deleteAllButton;
    private javax.swing.JCheckBox doubleBowlCheck;
    private javax.swing.JTextField inclinationText;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField radiousText;
    // End of variables declaration//GEN-END:variables
}
